package com.example.arm.common.lifecycle

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner

/**
 *  author : yanghaozhang
 *  date : 2022/12/27 14:04
 *  description :
 */

sealed class BaseLifecycleBinder(
    private val onCreateFunction: LifecycleCallBack? = null,
    private val onStartFunction: LifecycleCallBack? = null,
    private val onResumeFunction: LifecycleCallBack? = null,
    private val onPauseFunction: LifecycleCallBack? = null,
    private val onStopFunction: LifecycleCallBack? = null,
    private val onDestroyFunction: LifecycleCallBack? = null
) : DefaultLifecycleObserver {

    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        onCreateFunction?.invoke()
    }

    override fun onStart(owner: LifecycleOwner) {
        super.onStart(owner)
        onStartFunction?.invoke()
    }

    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)
        onResumeFunction?.invoke()
    }

    override fun onPause(owner: LifecycleOwner) {
        super.onPause(owner)
        onPauseFunction?.invoke()
    }

    override fun onStop(owner: LifecycleOwner) {
        super.onStop(owner)
        onStopFunction?.invoke()
    }

    override fun onDestroy(owner: LifecycleOwner) {
        super.onDestroy(owner)
        onDestroyFunction?.invoke()
        owner.lifecycle.removeObserver(this)
    }
}

class CreateBinder(onCreate: LifecycleCallBack?) : BaseLifecycleBinder(onCreate)
class StartBinder(onStart: LifecycleCallBack?) : BaseLifecycleBinder(onStartFunction = onStart)
class ResumeBinder(onResume: LifecycleCallBack?) : BaseLifecycleBinder(onResumeFunction = onResume)
class PauseBinder(onPause: LifecycleCallBack?) : BaseLifecycleBinder(onPauseFunction = onPause)
class StopBinder(onStop: LifecycleCallBack?) : BaseLifecycleBinder(onStopFunction = onStop)
class DestroyBinder(onDestroy: LifecycleCallBack?) : BaseLifecycleBinder(onDestroyFunction = onDestroy)